#!/bin/sh

orig_dir=`pwd`;

if test ! -f ./update_ver ; then
    cd "${0%%update_ver}";
fi

if test "x${DEBUG}" != "x" ; then
    echo "Starting $0 $*";
    set -x;
    pwd;
fi

\rm -f RCS_VERSION.java.r*
\rm -f RCS_VERSION.java.R*
\rm -f RCS_VERSION.java.[0-9]*
\rm -f RCS_VERSION.java.C*
\rm -f RCS_VERSION.java.mine

if test "x${DEBUG}" != "x" ; then
    echo DEVP_LIB_DIR="${DEVP_LIB_DIR}";
    ls -ld "${DEVP_LIB_DIR}";
fi
if test "x${DEVP_LIB_DIR}" = "x" ; then
    if test -d ../../../_build/plat/java/lib/ ; then
	DEVP_LIB_DIR=../../../_build/plat/java/lib/;
    elif test -d ../../../plat/java/lib/ ; then
	DEVP_LIB_DIR=../../../plat/java/lib/;
    fi
    if test "x${DEBUG}" != "x" ; then
        echo DEVP_LIB_DIR="${DEVP_LIB_DIR}";
        ls -ld "../../../_build/plat/java/lib/";
        ls -ld "../../../plat/java/lib/";
    fi
fi

if test "x${JAVAC}" = "x" ; then
    JAVAC=javac;
fi

if test -f ./RCS_VERSION.java.perm ; then
    
    if test -f RCS_VERSION.class ; then
	\rm -f RCS_VERSION.class
    fi

    rcsvers_hh=../../rcsvers.hh
    
    if test "x${DEBUG}" != "x" ; then
        echo TOP_SRCDIR="${TOP_SRCDIR}";
        ls -l "${TOP_SRCDIR}/src/rcsvers.hh";
    fi
    if test "x${TOP_SRCDIR}" != "x" -a -f "${TOP_SRCDIR}/src/rcsvers.hh" ; then
	rcsvers_hh=${TOP_SRCDIR}/src/rcsvers.hh
    fi
	
    if test -f "${rcsvers_hh}" ; then
    
	rcs_version=`cat "${rcsvers_hh}" | awk '{ if ( $1 == "#define" && $2 == "RCS_VERSION" ) print $3;}' | sed s/\"// | sed s/\"//`
	#svn_ver=`svnversion -n .` 2>/dev/null;
	if test "x${svn_ver}" != "x" ; then
	    rcs_version=`echo "${rcs_version}_${svn_ver}"`;
	fi
	date_var=`date`;
	if test "x${rcs_version}" != "x" -a "x${date_var}" != "x"  ; then
	    if test  -w . ; then
		if test "x${DEBUG}" != "x" ; then
		    pwd
		    ls -l
		    cat RCS_VERSION.java*;
		fi
		if test -f RCS_VERSION.java ; then
		    \rm -f RCS_VERSION.java
		fi
		cat < RCS_VERSION.java.perm | sed "s/RCS_VERSION_STRING/${rcs_version}/" | sed "s/RCS_VERSION_DATE/\"${date_var}\"/" >RCS_VERSION.java
	    else
		cat < RCS_VERSION.java.perm | sed "s/RCS_VERSION_STRING/${rcs_version}/" | sed "s/RCS_VERSION_DATE/\"${date_var}\"/" >/tmp/RCS_VERSION.java
		( cd /tmp ; ${JAVAC} RCS_VERSION.java ; )
		cp /tmp/RCS_VERSION.class ${DEVP_LIB_DIR} 
	    fi
	fi
    fi

fi

RCSLIB_MAVEN_SRC_DIR="../../../rcsjava_maven/rcslib/src/main/java/";

if test "x${DEBUG}" != "x" ; then
    pwd
    ls -l .
    ls -l "${RCSLIB_MAVEN_SRC_DIR}/rcs/"
fi

cp "${RCSLIB_MAVEN_SRC_DIR}/rcs/RCS_VERSION.java"  "/tmp/RCS_VERSION.java.${date_var}.backup"
cp ./RCS_VERSION.java "${RCSLIB_MAVEN_SRC_DIR}/rcs/"

if test "x${DEBUG}" != "x" ; then
    pwd
    ls -l .
    ls -l "${RCSLIB_MAVEN_SRC_DIR}/rcs/"
fi

cd "${orig_dir}"

if test "x${DEBUG}" != "x" ; then
    echo "Ending $0 $*";
fi
